﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace Web.FrmSetting
{
    public partial class user_left : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
            }
        }
        public string GetMenu()
        {
            StringBuilder str = new StringBuilder();
            DataTable dt = GetMenuTable(0);//获取根节点
            if (dt == null || dt.Rows.Count == 0) {
                return "";
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i]["ISShow"]) == 0)
                {
                    continue;//如果当前用户没有使用菜单的权限，则隐藏这一条菜单
                }
                str.Append(" <p class='menu_head'> " + dt.Rows[i]["strValue"].ToString() + "</p>");

                DataTable dt_child = GetMenuTable(Convert.ToInt32(dt.Rows[i]["ID"]));//获取子节点
                if (dt_child != null && dt_child.Rows.Count != 0)
                {
                    str.Append("  <div class='menu_body'>");
                    for (int j = 0; j < dt_child.Rows.Count; j++)
                    {
                        if (Convert.ToInt32(dt_child.Rows[j]["ISShow"]) == 0)
                        {
                            continue;//如果当前用户没有使用菜单的权限，则隐藏这一条菜单
                        }
                        DataTable dt_child_2 = GetMenuTable(Convert.ToInt32(dt_child.Rows[j]["ID"]));//后续子节点
                        if (dt_child_2 != null && dt_child_2.Rows.Count != 0)
                        {
                            str.Append(" <p class='menu_head_child'> " + dt_child.Rows[j]["strValue"].ToString() + "</p>");
                            str.Append("  <div class='menu_body_child'>");
                            for (int k = 0; k < dt_child_2.Rows.Count; k++)
                            {
                                if (Convert.ToInt32(dt_child_2.Rows[k]["ISShow"]) == 0)
                                {
                                    continue;//如果当前用户没有使用菜单的权限，则隐藏这一条菜单
                                }
                                string strValue = dt_child_2.Rows[k]["strValue"].ToString();
                                string strUrl = dt_child_2.Rows[k]["strUrl"].ToString();
                                string MenuID = dt_child_2.Rows[k]["ID"].ToString();
                                strUrl += "?MenuID=" + MenuID;
                                str.Append("<div class='menu_body_child_link'><a href='" + strUrl + "' target='user_main'>" + strValue + "</a></div>");
                            }
                            str.Append(" </div>");
                        }
                        else
                        {
                            string strValue = dt_child.Rows[j]["strValue"].ToString();
                            string strUrl = dt_child.Rows[j]["strUrl"].ToString();
                            string MenuID = dt_child.Rows[j]["ID"].ToString();
                            strUrl += "?MenuID=" + MenuID;
                            str.Append(" <div class='menu_body_link'> <a href='" + strUrl + "'  target='user_main'>" + strValue + "</a></div>");
                            // str.Append(" <a  href='<%=ResolveClientUrl("+strUrl+") %>'>" + strValue + "</a>");

                        }
                    }
                    str.Append(" </div>");
                }

            }
            return str.ToString();
        }

        DataTable GetMenuTable(int PID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("   SELECT ID,PID,strValue,strUrl,numSort,ISEnable");
            strSql.Append("  FROM Menu_User");
            strSql.Append("  where PID=@PID and ISEnable=1");
            strSql.Append("  order by numSort");

            SqlParameter para = new SqlParameter("@PID", PID);
            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString(), para);
            if (dt == null || dt.Rows.Count == 0)//Menu_User表没有菜单
            {
                return null;
            }
            dt.Columns.Add("ISShow",typeof(int));//是否要对用户显示此菜单(1:显示，2：不显示)
                 //查询当前用户的营业员的菜单
            string UserID = Session["ID"].ToString();
            object objmenuList = SQLHelper.ExecuteScalar(" SELECT menuList FROM UserMenu WHERE UserID=" + UserID + " AND numtype=2");
            if (objmenuList == null || objmenuList.ToString() == "")//userMenu表没有该用户的菜单记录
            {
                return null;
            }
            Dictionary<int, string> dicMenuList = new Dictionary<int, string>();
            if (objmenuList != null && objmenuList.ToString() != "")
            {
                string[] ArraymenuList = objmenuList.ToString().Split('|');
                for (int i = 0; i < ArraymenuList.Length; i++)
                {
                    string strmenu = ArraymenuList[i];
                    int menuKey = Convert.ToInt32(strmenu.Substring(0, strmenu.IndexOf(':')));
                    string menuValue = strmenu.Substring(strmenu.IndexOf(':') + 1);
                    dicMenuList.Add(menuKey, menuValue);
                }
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                
                int ID = Convert.ToInt32(dt.Rows[i]["ID"]);
                if (dicMenuList.Keys.Contains(ID))
                {
                    if (dicMenuList[ID].Contains("R"))
                    {
                        dt.Rows[i]["ISShow"] = 1;
                    }
                    else
                    {
                        dt.Rows[i]["ISShow"] = 0;
                    }
                   
                }
                else
                {
                    dt.Rows[i]["ISShow"] = 0;
                }
            }

            return dt;
        }

        public string GetNotice()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("   SELECT top 1 strContent FROM dbo.InfoNotice");
   
            object obj = SQLHelper.ExecuteScalar(strSql.ToString());
            if (obj == null)
            {
                return "";
            }
            else {
                return obj.ToString();
            }
        }
    }
}